<?php
/**
 * Created by PhpStorm.
 * User: stanley
 * Date: 2017/4/24 0024
 * Time: 9:01
 */

namespace app\Http\Controllers\Product;


use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    const FILE_MAX_SIZE = 2*1024 * 1024;

    function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if (!$menu_id) {
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('product.goods.list', compact('menu_id', 'toolBarButton'));
    }

    function listData()
    {
        $params['curr_menu_id'] = request('curr_menu_id', 0);
        if (!$params['curr_menu_id']) {
            return response()->json(['code' => 400, '页面无菜单ID']);
        }
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 10);
        try {
            $list = callApiServer("Product.GoodsService.getInfoList", $params);
            return response()->json($list);
        } catch (\Exception $e) {
            if ($e->getCode() == 500) {
                return response()->json(['code' => 400, 'data' => '后台服务异常:' . $e->getMessage()]);
            } else {
                return response()->json(['code' => 400, 'data' => $e->getMessage()]);
            }
        }
    }

    function showAddForm()
    {
        return view('product.goods.form');
    }

    function showEditForm()
    {
        $params['id'] = request('goods_id');
        try {
            $info = callApiServer("Product.GoodsService.getInfoById", $params);
            if (empty($info)) {
                return response()->json('对应数据不存在');
            }
            return view('product.goods.form', compact("info"));
        } catch (\Exception $e) {
            if ($e->getCode() == 500) {
                return response()->json(['code' => 500, 'data' => '后台服务异常:' . $e->getMessage()]);
            } else {
                return response()->json(['code' => 400, 'data' => $e->getMessage()]);
            }
        }
    }
    function changePrice()
    {
        $params['id'] = request('goods_id');
        try {
            $info = callApiServer("Product.GoodsService.getInfoById", $params);
            if (empty($info)) {
                return response()->json('对应数据不存在');
            }
            return view('product.goods.adjust', compact("info"));
        } catch (\Exception $e) {
            if ($e->getCode() == 500) {
                return response()->json(['code' => 500, 'data' => '后台服务异常:' . $e->getMessage()]);
            } else {
                return response()->json(['code' => 400, 'data' => $e->getMessage()]);
            }
        }
    }

    function save()
    {
        $params['id'] = request('id');
        $params['name'] = request('name');
        $params['category_id'] = request('category_id');
        $params['category_name'] = request('category_name');
        $params['brand_id'] = request('brand_id');
        $params['brand_name'] = request('brand_name');
        $params['label_ids'] = request('label_ids');
        $params['label_names'] = request('label_names');
        $params['goods_spec'] = request('goods_spec');
        $params['manufacturers_url'] = request('manufacturers_url');
        $params['goods_price'] = request('goods_price');
        $params['goods_state'] = request('goods_state');
        if(!empty($_FILES['goods_image'])){ //不为空才上传
            $file = $_FILES['goods_image'];
            list($result, $goods_image, $msg) = $this->upload($file, 'goods');
            if (!$result) {
                return response()->json(['code' => 400, 'data' => $msg]);
            }
            $params['goods_image'] = $goods_image;
        }
        if (empty($params['name'])) {
            return response()->json(['code' => 400, 'data' => '商品名称不能为空']);
        }
        if (empty($params['category_id'])) {
            return response()->json(['code' => 400, 'data' => '分类名称不能为空']);
        }
        if (empty($params['brand_id'])) {
            return response()->json(['code' => 400, 'data' => '品牌名称不能为空']);
        }
        if (empty($params['label_ids'])) {
            return response()->json(['code' => 400, 'data' => '标签不能为空']);
        }
        if (empty($params['goods_spec'])) {
            return response()->json(['code' => 400, 'data' => '商品规格不能为空']);
        }
        if (empty($params['manufacturers_url'])) {
            return response()->json(['code' => 400, 'data' => '制造商链接不能为空']);
        }
        if (empty($params['goods_price'])) {
            return response()->json(['code' => 400, 'data' => '商品价格不能为空']);
        }

        try {
            $rs = callApiServer("Product.GoodsService.saveData", $params);
        } catch (\Exception $e) {
            return response()->json(['code' => 400, 'data' => $e->getMessage()]);
        }
        if ($rs) {
            return response()->json(['code' => 200]);
        }
        return response()->json(['code' => 400, 'data' => $rs]);
    }

    private function upload($file, $type)
    {
        if ($file['error'] != 0) return [false, '', '上传失败'];
        if ($file['size'] > self::FILE_MAX_SIZE) return [false, '', '文件尺寸超过500k'];
        if (!getimagesize($file['tmp_name'])) return [false, '', '请上传正确格式的图片'];
        $name = md5($file['tmp_name'] . time());
        $ext = explode('/', $file['type'])[1];
        $dest = env('RESOURCE_PATH') . '/' . $type . '/' . date('ymd');
        if (!is_dir($dest)) {
            mkdir($dest, 0777, true);
            chmod($dest, 0777);
        }
        $r = move_uploaded_file($file['tmp_name'], $dest . '/' . $name . '.' . $ext);
        if ($r) {
            return [true, '/' . $type . '/' . date('ymd') . '/' . $name . '.' . $ext, 'success'];
        } else {
            return [false, '', '上传失败'];
        }
    }
    public function adjust(){
        $params['id'] = request('id');
        $params['goods_price'] = request('goods_price');
        if (empty($params['goods_price'])) {
            return response()->json(['code' => 400, 'data' => '商品价格不能为空']);
        }

        try {
            $rs = callApiServer("Product.GoodsService.adjust", $params);
        } catch (\Exception $e) {
            return response()->json(['code' => 400, 'data' => $e->getMessage()]);
        }
        if ($rs) {
            return response()->json(['code' => 200]);
        }
        return response()->json(['code' => 400, 'data' => $rs]);
    }

    public function goodsAjax()
    {
        $params['key'] = request('q');

        $rs = callApiServer("Product.GoodsService.getGoodsList", $params);
        return response()->json($rs);
    }
}